package adapter;

import java.util.ArrayList;

import model.Comment;
import model.Video;

import com.adgroup.doremon_ad.R;
import com.androidquery.AQuery;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AdapterComment extends BaseAdapter{

	ArrayList<Comment> arrs = new ArrayList<Comment>();
	Context context;
	
	public AdapterComment(ArrayList<Comment> arrs, Context c){
		this.arrs = arrs;
		this.context = c;		
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arrs.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arrs.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {
		Holder holder;
		LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		if (arg1 == null) {
			holder = new Holder();
			arg1 = inflater.inflate(R.layout.item_comment, null);
			holder.tvUser = (TextView) arg1.findViewById(R.id.txtUsername_commentrow);
			holder.tvDate = (TextView) arg1.findViewById(R.id.txtDatetime_commentrow);
			holder.tvComment = (TextView) arg1.findViewById(R.id.txtcomment_commentrow);
			
			arg1.setTag(holder); 
		} else {
			holder = (Holder) arg1.getTag();
		}
		final Comment comment = arrs.get(arg0);
		holder.tvComment.setText(comment.getComment());
		String dateTime = comment.getDatepublish();
		dateTime = dateTime.substring(0,dateTime.lastIndexOf(":"));
		holder.tvDate.setText(dateTime);
		holder.tvUser.setText(comment.getNickname());
		return arg1;
	}
	
	private class Holder
	{
		TextView tvUser, tvDate, tvComment;		
	}

	public void updateChart(ArrayList<Comment> arr) {
		for (Comment broadcasting : arr) {
			arrs.add(broadcasting);			
		}
		this.notifyDataSetChanged();
	}

	public void resetChart(ArrayList<Comment> arr) {
		arrs.clear();
		arrs = arr;
		this.notifyDataSetChanged();
	}
}
